home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCollec / Include / FWTMap.tpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  4.7 KB  |  141 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWTMap.tpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWTMap.h"
  11.  
  12. //========================================================================================
  13. // Class FW_TMap
  14. //========================================================================================
  15.  
  16. //----------------------------------------------------------------------------------------
  17. // FW_TMap<tKey, tValue>::FW_TMap
  18. //----------------------------------------------------------------------------------------
  19.  
  20. template<class tKey, class tValue>
  21. inline FW_TMap<tKey, tValue>::FW_TMap(FW_SortedArray_CompareFunction compare)
  22.     : fRep(0)
  23. {
  24.     FW_PlatformError error;
  25.     fRep = FW_PrivSortedArray_New(&error, compare);
  26.     FW_FailOnError(error);
  27. }
  28.  
  29. //----------------------------------------------------------------------------------------
  30. // FW_TMap<tKey, tValue>::FW_TMap
  31. //----------------------------------------------------------------------------------------
  32.  
  33. template<class tKey, class tValue>
  34. inline FW_TMap<tKey, tValue>::~FW_TMap()
  35. {
  36.     for (long i=GetLength()-1; i>=0; --i)
  37.     {
  38.         FW_TPair<tKey, tValue>* item = GetItemAt(i);
  39.         delete item;
  40.     }
  41.     FW_PrivSortedArray_Dispose(fRep);
  42. }
  43.  
  44. //----------------------------------------------------------------------------------------
  45. // FW_TMap<tKey, tValue>::GetLength
  46. //----------------------------------------------------------------------------------------
  47.  
  48. template<class tKey, class tValue>
  49. inline long FW_TMap<tKey, tValue>::GetLength() const
  50. {
  51.     return FW_PrivSortedArray_GetLength(fRep);
  52. }
  53.  
  54. //----------------------------------------------------------------------------------------
  55. // FW_TMap<tKey, tValue>::GetItemAt
  56. //----------------------------------------------------------------------------------------
  57.  
  58. template<class tKey, class tValue>
  59. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::GetItemAt(long index) const
  60. {
  61.     return (FW_TPair<tKey, tValue>*) FW_PrivSortedArray_GetItemAt(fRep, index);
  62. }
  63.  
  64. //----------------------------------------------------------------------------------------
  65. // FW_TMap<tKey, tValue>::Find
  66. //----------------------------------------------------------------------------------------
  67.  
  68. template<class tKey, class tValue>
  69. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Find(FW_TPair<tKey, tValue>* item) const
  70. {
  71.     FW_TPair<tKey, tValue>* result = 0;
  72.     long index;
  73.     FW_Boolean found = FW_PrivSortedArray_Find(fRep, item, &index);
  74.     if (found)
  75.         result = GetItemAt(index);
  76.     return result;
  77. }
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // FW_TMap<tKey, tValue>::Find
  81. //----------------------------------------------------------------------------------------
  82.  
  83. template<class tKey, class tValue>
  84. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Find(const tKey& key) const
  85. {
  86.     return Find(&FW_TPair<tKey, tValue>(key));
  87. }
  88.  
  89. //----------------------------------------------------------------------------------------
  90. // FW_TMap<tKey, tValue>::Add
  91. //----------------------------------------------------------------------------------------
  92.  
  93. template<class tKey, class tValue>
  94. inline FW_TPair<tKey, tValue>*  FW_TMap<tKey, tValue>::Add(FW_TPair<tKey, tValue>* newItem)
  95. {
  96.     long index;
  97.     FW_PlatformError error;
  98.     FW_PrivSortedArray_Add(fRep, newItem, &index, &error);
  99.     FW_FailOnError(error);
  100.     return newItem;
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_TMap<tKey, tValue>::Add
  105. //----------------------------------------------------------------------------------------
  106.  
  107. template<class tKey, class tValue>
  108. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Add(const tKey& key, const tValue& value)
  109. {
  110.     FW_PlatformError error;
  111.     long index;
  112.     FW_TPair<tKey, tValue>* newItem = new FW_TPair<tKey, tValue>(key, value);
  113.     FW_PrivSortedArray_Add(fRep, newItem, &index, &error);
  114.     FW_FailOnError(error);
  115.     return newItem;
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. // FW_TMap<tKey, tValue>::operator[]
  120. //----------------------------------------------------------------------------------------
  121.  
  122. template<class tKey, class tValue>
  123. inline tValue& FW_TMap<tKey, tValue>::operator[](const tKey& key)
  124. {
  125.     FW_PlatformError error;
  126.     FW_TPair<tKey, tValue>* item = 0;
  127.     long index;
  128.     FW_Boolean found = FW_PrivSortedArray_Find(fRep, &FW_TPair<tKey, tValue>(key), &index);
  129.     if (found)
  130.         item = GetItemAt(index);
  131.     else
  132.     {
  133.         item = new FW_TPair<tKey, tValue>(key);
  134.         FW_PrivSortedArray_Add(fRep, item, &index, &error);
  135.         FW_FailOnError(error);
  136.     }
  137.     FW_ASSERT(item != NULL);
  138.     return item->fValue;
  139. }
  140.  
  141.